var sorl = sorl || {};
sorl.curator = sorl.curator || {};

sorl.curator.DDApp = {
    lists: [],

    init: function() {
        this.lists = $('#reorder .reorderlist');
        for (var i=0; i<this.lists.length; i++) {
            new YAHOO.util.DDTarget(this.lists[i]);
            nodes = this.lists[i].getElementsByTagName('li');
            for (var j=0; j<nodes.length; j++) {
                new sorl.curator.DDList(nodes[j].id);
            }
        }

    },

    save: function() {
        var order = {};
        for (var i=0; i<this.lists.length; i++) {
            var pks = [];
            var nodes = this.lists[i].getElementsByTagName('li');
            for (var j=0; j<nodes.length; j++) {
                pk = nodes[j].id.replace(/\w+_/, '');
                pks.push(pk);
            }
            var list_key = this.lists[i].id.replace(/\w+_/, '');
            order[list_key] = pks;
        }
        var callback = {};
        callback.success = function(o) {
            /*
             * this makes the page jump
             *
            var message_list = $('.messagelist')[0];
            if (!message_list) {
                bread = $('.breadcrumbs')[0];
                message_list = document.createElement('ul');
                message_list.className = "messagelist";
                var message_item = document.createElement('li');
                var message = document.createTextNode(o.responseText);
                message_item.appendChild(message);
                message_list.appendChild(message_item);
                bread.parentNode.insertBefore(message_list, bread.nextSibling);
            } else {
                Dom.setStyle(message_list, 'opacity', 1);
            }
            var attributes = {opacity: {to: 0}};
            var anim = new YAHOO.util.Anim(message_list, attributes);
            anim.animate();
            */
        };
        callback.failure = function(o) {document.write(o.responseText)};
    
        postData    = "order=" + JSON.stringify(order);
        transaction = Connect.asyncRequest('POST', ".", callback, postData);
    }

};

Event.onDOMReady(sorl.curator.DDApp.init, sorl.curator.DDApp, true);
